//
//  SNDeteSelectView.m
//  app
//
//  Created by 王少宁 on 15/8/31.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNDeteSelectView.h"

@interface SNDeteSelectView ()
@property (nonatomic, retain) UILabel * currentDateLabel;
@property (nonatomic, retain) UIDatePicker * datePicker;
@property (nonatomic, assign) BOOL confirm;
@property (nonatomic, assign) UIButton * bespeakTimeBtn;
@property (nonatomic, retain) NSDate * bespeakTimeBtnOneDate;
@property (nonatomic, retain) NSDate * bespeakTimeBtnTwoDate;
@end

@implementation SNDeteSelectView

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]) {
        self.backgroundColor = [UIColor clearColor];
        UIView * backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenHeight)];
        backGroundView.backgroundColor = [UIColor blackColor];
        backGroundView.alpha = 0.4;
        [self addSubview:backGroundView];
        [self createDeteSelectViewWithFrame:frame];
        self.confirm = NO;
        self.hidden = YES;
    }
    return self;
}

- (void)createDeteSelectViewWithFrame:(CGRect)frame
{
    UIView * selectBgdView = [[UIView alloc] initWithFrame:frame];
    selectBgdView.backgroundColor = [UIColor whiteColor];
    [self addSubview:selectBgdView];
    
    _currentDateLabel = CREATE_WITH_FRAME_OF(UILabel, [selectBgdView.ubo fx:0 fy:0],
                                             [selectBgdView.ubo fw:1.0 oh:50]);
    [selectBgdView addSubview:_currentDateLabel];
    
    UILabel * lineLabel1 = CREATE_WITH_FRAME_OF(UILabel, [_currentDateLabel.ufo fx:0 fy:1.0],
                                               [_currentDateLabel.ubo fw:1.0 oh:2]);
    _currentDateLabel.textColor = lineLabel1.backgroundColor = [XCommon hexStringToColor:@"#32b1e6"];
    [selectBgdView addSubview:lineLabel1];
    
    _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(-selectBgdView.frame.origin.x, _currentDateLabel.ufo.yy, ScreenWidth, 216)];
    [selectBgdView addSubview:_datePicker];
    
    UILabel * lineLabel2 = CREATE_WITH_FRAME_OF(UILabel, [self.ufo ox:_currentDateLabel.ufo.x oy:_datePicker.ufo.yy],
                                                [_currentDateLabel.ubo fw:1.0 ph:1]);
    [selectBgdView addSubview:lineLabel2];
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectOf([lineLabel2.ufo fx:0 fy:1.0],
                               [lineLabel2.ubo fw:0.5 pw:-1 oh:40]);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancelBtn.tag = 1;
    [cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [selectBgdView addSubview:cancelBtn];
    
    UILabel * lineLabel3 = CREATE_WITH_FRAME_OF(UILabel, [cancelBtn.ufo fx:1.0 fy:0],
                                                [cancelBtn.ubo pw:1 fh:1.0]);
    lineLabel3.backgroundColor = lineLabel2.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [selectBgdView addSubview:lineLabel3];
    
    UIButton * confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectOf([lineLabel3.ufo fx:1.0 fy:0],
                               [cancelBtn.ubo fw:1.0 fh:1.0]);
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    confirmBtn.tag = 2;
    [confirmBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [selectBgdView addSubview:confirmBtn];
}

- (void)showWithBtn:(UIButton *)btn
{
    self.hidden = NO;
    
    NSDateFormatter * formagtter= [[NSDateFormatter alloc] init];
    formagtter.dateFormat = @"yyyy-MM-dd HH:mm";
    _currentDateLabel.text = [NSString stringWithFormat:@"  %@",[formagtter stringFromDate:[NSDate date]]];
    
    if (btn.tag == 1) {
        _datePicker.minimumDate = [NSDate date];
        if (self.bespeakTimeBtnTwoDate) {
            _datePicker.maximumDate = self.bespeakTimeBtnTwoDate;
        }
    } else if (btn.tag == 2) {
        if (self.bespeakTimeBtnOneDate) {
            _datePicker.minimumDate = self.bespeakTimeBtnOneDate;
        }
    }
    
    self.bespeakTimeBtn = btn;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.hidden = YES;
}

- (void)btnClick:(UIButton *)btn
{
    if (btn.tag == 2) {     //确定
        NSDateFormatter * formagtter= [[NSDateFormatter alloc] init];
        formagtter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSString * date = [formagtter stringFromDate:_datePicker.date];
        [self.bespeakTimeBtn setTitle:date forState:UIControlStateNormal];
        if (self.bespeakTimeBtn.tag == 1) {
            _bespeakTimeBtnOneDate = _datePicker.date;
        } else if (self.bespeakTimeBtn.tag == 2) {
            _bespeakTimeBtnTwoDate = _datePicker.date;
        }
    }
    
    self.hidden = YES;
}

@end
